package com.example.idea.controller;

import com.example.idea.entity.Logs;
import com.example.idea.service.LogsService;
import com.example.idea.utils.Page;
import com.example.idea.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/logs")
public class LogsController {

    @Resource
    private LogsService logsService;

    /**
     * 添加新闻
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Logs logs) {
        logsService.add(logs);
        return Result.success(logs);
    }

    /**
     * 修改新闻
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Logs logs) {
        log.info("修改用户:{}", logs);
        logsService.update(logs);
        return Result.success(logs);
    }

    /**
     * 删除新闻
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        logsService.delete(id);
        return Result.success(null);
    }

    /**
     * 根据ID查询新闻
     */
    @RequestMapping("/query/{id}")
    public Result query(@RequestParam Integer id) {
        Logs logs = logsService.query(id);
        return Result.success(logs);
    }

    /**
     * 批量删除新闻语句
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        logsService.deleteBatch(ids);
        return Result.success(null);
    }

    /**
     * 查询全部新闻信息
     */
    @RequestMapping("/list")
    public Result list() {
        log.info("查询全部用户信息");
        List<Logs> logs = logsService.list();
        return Result.success(logs);
    }

    /**
     * 根据不同条件进行查询数据
     */
    @RequestMapping("/selectList")
    public Result list(@RequestBody Logs logs) {
        List<Logs> logs1 = logsService.selectList(logs);
        return Result.success(logs1);
    }

    @RequestMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String operation,
                               @RequestParam String type) {
        log.info("分页查询，页码：{},页大小：{},用户名：{},姓名：{}", pageNum, pageSize, operation, type);
        Page<Logs> page = logsService.selectByPage(pageNum, pageSize, operation, type);
        return Result.success(page);
    }
}
